﻿package com.student.dao.impl;

import java.util.List;


import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.student.dao.IHibernateDao;
import com.student.dao.LoginDao;
import com.student.model.Colleges;
import com.student.model.Tuser;
import com.student.model.UserType;
import com.student.util.UserRoles;

/**用户登录DAO实现
 * @author 陈伟斌
 *
 */
@Component("LoginDao")
public class LoginDaoImpl extends HibernateDaoImpl implements LoginDao {

	public List<Colleges> getCollegesList() {
		String hql="from Colleges c";
		return this.find(hql);
	}

	public Tuser validateLoginUser(String userName, String password,
			String userRoles, Long collegeId) throws Exception{
		try {
			String hql="from Tuser u where u.userType.typeName='"+userRoles+"' and u.userName='"+userName+"'";
			if(collegeId!=null){//指定学院（只有二级学院登陆时才需要指定学院）
				hql+=" and u.colleges.collegeId="+collegeId;
			}
			hql+=" or ( u.userName='"+userName+"' and u.userType.typeName='"+UserRoles.SUPER_ADMIN+"') ";
			hql+=" or ( u.userName='"+userName+"' and u.userType.typeName='"+UserRoles.SPECIAL_USER+"') ";
			List<Tuser> users=this.getSession().createQuery(hql).list();
			if(users==null||users.size()<1)
				return null;
			Tuser user=users.get(0);
			if(!user.getUserPassword().equals(password.trim()))
				return  null ;
			return user;
		} catch (Exception e) {
			
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	public Colleges getcCollegeById(Long collegeId) {
		
		return (Colleges) this.get(Colleges.class, collegeId);
	}


	



}
